/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core;

import com.inspur.edp.bef.bizentity.GspBusinessEntity;
import com.inspur.edp.bef.bizentity.beenum.BECategory;
import com.inspur.edp.bef.dtgenerator.BEGeneratorContext;
import com.inspur.edp.bef.dtgenerator.DotNetToJavaStringHelper;
import com.inspur.edp.bef.dtgenerator.core.config.BefConfigClassGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.managergen.BefManagerGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.repositorygen.BefRepositoryGenerator;
import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.cef.entity.config.CefConfig;
import com.inspur.edp.cef.generator.coreGenerator.base.CefCoreGeneratorContext;
import com.inspur.edp.cef.generator.coreGenerator.base.configGenerator.ConfigClassGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator.ManagerGenerator;
import com.inspur.edp.cef.generator.coreGenerator.entityGenerator.CommonModelCoreGenerator;
import com.inspur.edp.cef.generator.coreGenerator.entityGenerator.CommonModelCoreGeneratorContext;
import com.inspur.edp.cef.generator.repository.RepositoryGenerator;
import com.inspur.edp.das.commonmodel.entity.GspCommonModel;

public class BefCoreGenerator extends CommonModelCoreGenerator {
  public BefCoreGenerator(BEGeneratorContext beGenCtx) {
    super(beGenCtx, beGenCtx.getBE());
  }

  @Override
  public BefCoreGenCtx getContext() {
    return (BefCoreGenCtx) super.getContext();
  }

  private BEGeneratorContext getBeGenCtx() {
    return (BEGeneratorContext) super.cmGenCtx;
  }

  @Override
  protected boolean getHasConfigGenerator() {
    BefCoreGenCtx befCoreGenCtx = getContext();
    if (befCoreGenCtx != null && befCoreGenCtx.getCommonModel() != null) {
      return !(befCoreGenCtx.getCommonModel().isSimplifyGen() || befCoreGenCtx.getCommonModel().isSimpBeanConfig());
    }
    return true;
  }

  @Override
  protected boolean getHasRepository() {
    return getContext().getBE().getCategory() != BECategory.DependentBusinessEntity;
  }

  @Override
  protected CefConfig buildConfigInfo() {
    CefConfig config = new CefConfig();
    GspBusinessEntity be = (GspBusinessEntity) getContext().getCefGenContext().getMetadata().getContent();
    config.setID(be.getGeneratedConfigID());
    config.setBEID(be.getID());
    String nameSpace = be.getGeneratingAssembly() + be.getCode() + ".core";
    config.setDefaultNamespace(nameSpace.toLowerCase());
    return config;
  }

  @Override
  protected RepositoryGenerator getRepositoryGenerator() {
    return new BefRepositoryGenerator(getContext());
  }

//  private BefCoreGenContext getBefCoreGenContext(){
//    return new BefCoreGenContext(getContext().getCefGenContext(), getBeGenCtx().getCommonDataType());
//  };

  @Override
  protected CommonModelCoreGeneratorContext createEntityCoreGenContext() {
    return new BefCoreGenCtx(getBeGenCtx());
  }

  @Override
  protected ConfigClassGenerator getConfigGenerator() {
    return new BefConfigClassGenerator(getContext());
  }

  @Override
  protected ManagerGenerator createMgrImplGenerator() {
    return new BefManagerGenerator(getContext());
  }

  @Override
  protected void doInitialize() {
    super.doInitialize();
    if (!DotNetToJavaStringHelper.isNullOrEmpty(
        getContext().getBeGenCtx().getBE().getDependentEntityId())) {
      getContext()
          .setParentNamespace(CoreHelper.getNamespace(getContext().getBeGenCtx().getParentBE()));
    getContext().setParentAssemblyName(getContext().getParentNamespace());
    }
  }

  //  	@Override
//	protected void addExtendChildGenerators(java.util.ArrayList<BaseGenerator> childGenerators)
//	{
////		if (getBefGenContext().getCefConfig().getRepositoryConfig() != null)
////		{
////			childGenerators.add(new BefRepositoryGenerator(getBeGenCtx()));
////		}
////
////		ProjectInfoGenerator varGenerator = CreateVariableGenerator();
////		if (varGenerator != null)
////		{
////			childGenerators.add(varGenerator);
////		}
//	}
}
